/**
 * 
 */
package stc.skymobi.zookeeper;

import java.io.IOException;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;

/**
 * @author hp
 *
 */
public class ZooKeeperBooter {
	
	private	ZooKeeper	zk;

	private	String		connectString;
	private	int 		sessionTimeout;

	public	ZooKeeperOperations	getZooKeeperOperations() {
		return	new ZooKeeperAdapter().setZk(zk);
	}
	
	/**
	 * @return the connectString
	 */
	public String getConnectString() {
		return connectString;
	}
	/**
	 * @param connectString the connectString to set
	 */
	public void setConnectString(String connectString) {
		this.connectString = connectString;
	}
	/**
	 * @return the sessionTimeout
	 */
	public int getSessionTimeout() {
		return sessionTimeout;
	}
	/**
	 * @param sessionTimeout the sessionTimeout to set
	 */
	public void setSessionTimeout(int sessionTimeout) {
		this.sessionTimeout = sessionTimeout;
	}
	
	public void start() throws IOException {
		zk = new ZooKeeper(connectString, sessionTimeout, new Watcher(){

			public void process(WatchedEvent event) {
				if ( event.getType().equals(EventType.None) ) {
					System.out.println(event);
				}
			}} );
	}
	
	public void stop() throws InterruptedException {
		zk.close();
	}

	/**
	 * @return
	 * @see org.apache.zookeeper.ZooKeeper#getSessionId()
	 */
	public long getSessionId() {
		return zk.getSessionId();
	}

	/**
	 * @return
	 * @see org.apache.zookeeper.ZooKeeper#getSessionPasswd()
	 */
	public byte[] getSessionPasswd() {
		return zk.getSessionPasswd();
	}

	/**
	 * @return
	 * @see org.apache.zookeeper.ZooKeeper#getState()
	 */
	public String getState() {
		return zk.getState().toString();
	}

}
